import Vue from 'vue'
import refreshUniacInfo from "./refreshUniacInfo";
import siteinfo from "../siteinfo.js"; // 工具类

//封装请求函数
async function POST(url, data) {
	// #ifdef H5
	await refreshUniacInfo();
	parseUrl(url);
	url = parseUrl(url);
	// #endif
	let httpurl = siteinfo.siteroot;
	const promise1 = new Promise((resolve, reject) => {
		//网络请求
		uni.showLoading({
			title: "......",
		});

		uni.request({
			url: httpurl + `${url}`,
			data: data,
			method: "POST",
			header: {
				platform: "H5",
			},

			success(res) {
				uni.hideLoading({
					success: (res) => {},
				});

				if (res.data.code == -1) {
					console.log("登录返回", res)
					uni.removeStorageSync("token");
					uni.removeStorageSync("userInfo");
					//  "您的登录状态已过期，请重新登录！"
					uni.showToast({
						title:res.data.msg,
						icon:'none',
						duration:3000
					})
					// setTimeout(()=>{
						
					// },700)
					return
					uni.showModal({
						title: '',
						content:res.data.msg,
						showCancel: false,
						confirmText: "去登录 go Login",
						success(res) {
							if (res.confirm) {
								// console.log('用户点击确定')
								uni.reLaunch({
									url: "/pages/my/my",
								});
							}
						},
					});
				}

				resolve(res);
			},

			fail(error) {
				uni.showToast({
					title: "网络似乎开小差了",
					icon: "none",
					duration: 2000,
				});
				reject(error);
			},
		});
		// wx.hideLoading();
	});
	return promise1;
} //封装请求函数

async function GET(url, data) {
	// #ifdef H5
	await refreshUniacInfo();
	parseUrl(url);
	url = parseUrl(url);
	// #endif
	let httpurl = siteinfo.siteroot;
	const promise1 = new Promise((resolve, reject) => {
		//网络请求
		uni.showLoading({
			title: "......",
		});
		uni.request({
			url: httpurl + `${url}`,
			data: data,
			method: "GET",
			header: {
				platform: "H5",
			},

			success(res) {
				uni.hideLoading({
					success: (res) => {},
				}); // console.log(res)

				if (res.data.code == -1) {
					// content: "您的登录状态已过期，请重新登录！",
					console.log("登录返回2", res.data.msg)
					uni.removeStorageSync("token");
					uni.removeStorageSync("userInfo");
					uni.showToast({
						title:res.data.msg,
						icon:'none',
						duration:2000
					})
					return
					uni.showModal({
						title:res.data.msg,
						// content:res.data.msg,
						showCancel: false,
						confirmText: "去登录 go Login",

						success(res) {
							if (res.confirm) {
								// console.log('用户点击确定')
								uni.reLaunch({
									url: "/pages/my/my",
								});
							}
						},
					});
				}

				resolve(res);
			},

			fail(error) {
				uni.showToast({
					title: "网络似乎开小差了",
					icon: "none",
					duration: 2000,
				});
				reject(error);
			},
		});
	});
	return promise1;
}

// 参数替换
function parseUrl(url) {
	// console.log(url,889)
	try {
		const u = url.split("?");
		const o = u[1]?.split("&");
		const p_url = o.map((e) => {
			if (e.includes("wxapp_id=")) {
				e = "wxapp_id=" + uni.getStorageSync("web_id");
			}
			return e;
		});

		return `${u[0]}?${p_url.join("&")}`;
	} catch (error) {
		return url;
	}
}

module.exports = {
	post: POST,
	get: GET,
};